﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using connector = Microsoft.Office.Server.Search.Connector;
using Microsoft.Office.Server.Search.Connector.BDC;
using Microsoft.BusinessData.MetadataModel;
using System.IO;
using Microsoft.SharePoint.Utilities;
using MsExchangeBcsConnector.ExchangeBdcModel;
using MsExchangeBcsConnector.ExchangeBdcModel.Helpers;

namespace MsExchangeBcsConnector
{

    /// <summary>
    /// Defines methods that map the URL coming in from search to the Object in Metadata store
    /// </summary>
    class MsExchangeLobUri : LobUri
    {

        public MsExchangeLobUri() : base(Constants.SearchProtocolName) //myfile is the scheme of the URLs
        {
            this.lobSystem = this.Catalog.GetLobSystem(Constants.LobSystemName); //Name of LobSystem defined in model file
            // We have only one LobSystemInstance defined in our model file
            this.lobSystemInstance = this.lobSystem.GetLobSystemInstances()[0].Value;
        }

        public override void Initialize(Microsoft.Office.Server.Search.Connector.IConnectionContext context)
        {
            this.sourceUri = context.Path;

            if (string.IsNullOrEmpty(context.Path.Host))
            {
                LoggingService.LogCritical(@"Illegal start address {0}. Address should be in format msews://<msexchangeinstance>, where <msexchangeinstance> is the name of your BCS instance name corresponding to Exchange Server", context.Path.OriginalString);
                throw new ArgumentException(@"Illegal start address {0}. Address should be in format msews://<msexchangeinstance>, where <msexchangeinstance> is the name of your BCS instance name corresponding to Exchange Server", context.Path.OriginalString);
            }

            SearchConnectorUriParser uriParser = SearchConnectorUriParser.ParseAndValidateUriParams(this.sourceUri);

            if (uriParser.EntityType == SearchConnectorEntityType.Root)
            {
                this.entity = this.Catalog.GetEntity(Constants.BcsNamespace, Constants.MailBoxEntityName);                
            }
            else if (uriParser.EntityType == SearchConnectorEntityType.MailBox)
            {
                this.entity = this.Catalog.GetEntity(Constants.BcsNamespace, Constants.MailBoxEntityName);
                this.identity = new Microsoft.BusinessData.Runtime.Identity(uriParser.EMail);
            }else if (uriParser.EntityType == SearchConnectorEntityType.MailFolder)
            {
                this.entity = this.Catalog.GetEntity(Constants.BcsNamespace, Constants.MailFolderEntityName);              
                this.identity = new Microsoft.BusinessData.Runtime.Identity(new object[] { uriParser.MailFolderID, uriParser.EMail });
            }
            else if (uriParser.EntityType == SearchConnectorEntityType.Message)
            {
                this.entity = this.Catalog.GetEntity(Constants.BcsNamespace, Constants.MessageEntityName);
                this.identity = new Microsoft.BusinessData.Runtime.Identity(new object[] {uriParser.MessageID, uriParser.EMail});
            }
            else if (uriParser.EntityType == SearchConnectorEntityType.Attachment)
            {
                this.entity = this.Catalog.GetEntity(Constants.BcsNamespace, Constants.AttachmentEntityName);
                this.identity = new Microsoft.BusinessData.Runtime.Identity(new object[] { uriParser.AttachmentID, uriParser.MessageID, uriParser.EMail });
            }
            else
            {
                LoggingService.LogCritical(@"Incorrect or unknown request format {0}.", this.sourceUri);                
            }
            
        }


        private IEntity entity;
        public override Microsoft.BusinessData.MetadataModel.IEntity Entity
        {
            get { return this.entity; }
        }

        private Microsoft.BusinessData.Runtime.Identity identity;
        public override Microsoft.BusinessData.Runtime.Identity Identity
        {
            get { return this.identity; }
        }


        private ILobSystem lobSystem;
        public override Microsoft.BusinessData.MetadataModel.ILobSystem LobSystem
        {
            get { return this.lobSystem; }
        }

        private ILobSystemInstance lobSystemInstance;
        public override Microsoft.BusinessData.MetadataModel.ILobSystemInstance LobSystemInstance
        {
            get { return this.lobSystemInstance; }
        }

        private Uri sourceUri;
        public override Uri SourceUri
        {
            get
            {
                return this.sourceUri;
            }
            set
            {
                this.sourceUri = value;
            }
        }

        public override Guid PartitionId
        {
            get { throw new NotImplementedException(); }
        }
    }
}
